import os
import unittest
from pathlib import Path

from video_summarize_tools.tools.modelscope_service import ModelScopeService


class ModelScopeService_test(unittest.TestCase):
    def setUp(self):
        """初始化服务类实例"""
        api_key = os.getenv("api-key")
        self.service = ModelScopeService(api_key)


    def test_transcript_audio(self):
        api_key = os.getenv("api-key")
        service = ModelScopeService(api_key)
        service.transcript_audio(Path("C:\\Users\\zst\\AppData\\Local\\Temp\\50341acc-792e-4cec-a938-0f0a8d1685cf\\f96-13.mp4_audio"))


    def test_video_understanding(self):
        api_key = os.getenv("api-key")
        service = ModelScopeService(api_key)

        dir = Path("C:\\Users\\zst\\AppData\\Local\\Temp\\e90f8268-7c52-468f-931c-41a86b9ebeae")
        l = list(dir.iterdir())
        l.sort(key=lambda x: int(x.name.split(".")[0].split("_")[1]))

        result = service.video_understanding(l, fps=1)
        print(result)

    # def test_transcript_audio(self):
    #     api_key = os.getenv("api-key")
    #     service = ModelScopeService(api_key)
    #
    #     dir = Path("C:\\Users\\zst\\AppData\\Local\\Temp\\e90f8268-7c52-468f-931c-41a86b9ebeae")

    def test_chat(self):
        api_key = os.getenv("api-key")
        service = ModelScopeService(api_key)

        response = service.chat(prompt="hello")
        print(response)

    def test_translate_bbox_normal_case(self):
        """正常缩放测试"""
        bbox = (500, 500, 800, 800)
        image_coord = (1920, 1080)
        expected = (960, 540, 1536, 864)
        result = self.service.translate_bbox_to_image_coord(bbox, image_coord)
        self.assertEqual(result, expected)

    def test_translate_bbox_with_rounding_down(self):
        """包含向下取整的情况"""
        bbox = (100, 200, 700, 900)
        image_coord = (640, 480)
        expected = (64, 96, 448, 432)
        result = self.service.translate_bbox_to_image_coord(bbox, image_coord)
        self.assertEqual(result, expected)

    def test_translate_bbox_zero_coordinates(self):
        """原点坐标的转换测试"""
        bbox = (0, 0, 0, 0)
        image_coord = (1280, 720)
        expected = (0, 0, 0, 0)
        result = self.service.translate_bbox_to_image_coord(bbox, image_coord)
        self.assertEqual(result, expected)

    def test_translate_bbox_maximum_values(self):
        """最大边界值测试"""
        bbox = (1000, 1000, 1000, 1000)
        image_coord = (1280, 720)
        expected = (1280, 720, 1280, 720)
        result = self.service.translate_bbox_to_image_coord(bbox, image_coord)
        self.assertEqual(result, expected)

    def test_translate_bbox_small_image_size(self):
        """小图尺寸下的缩放测试"""
        bbox = (500, 500, 800, 800)
        image_coord = (800, 600)
        expected = (400, 300, 640, 480)
        result = self.service.translate_bbox_to_image_coord(bbox, image_coord)
        self.assertEqual(result, expected)
